package com.ciko.code.http;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.orhanobut.logger.Logger;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class TJBaseHttpHandler extends AsyncHttpResponseHandler {

    private final String TAG = getClass().getSimpleName();
    private HttpResponseListener listener;
    private String requestTag;
    private Gson gson;
    private Class<?> clazz;

    public TJBaseHttpHandler(HttpResponseListener listener, String tag, Class<?> clazz) {
        this.listener = listener;
        this.requestTag = tag;
        this.clazz = clazz;
        gson = new Gson();
    }

    @Override
    public void onSuccess(final int status, final Header[] headers, final byte[] body) {

        String data = body == null ? "" : new String(body);

        if (status == -1) {
            Logger.t(requestTag).d("接口" + requestTag + ">>>>>>请求开始:");
            listener.onRequestStart(requestTag);
        } else {
            try {

                Logger.t(requestTag).d("接口" + requestTag + ">>>>>>请求结果 : ");
                Logger.t(requestTag).json(data.trim());


                JSONObject object = new JSONObject(data);
                int code = object.getInt("resultCode");
                if(code == 1000){
                    BaseResponse<?> resp = (BaseResponse<?>) gson.fromJson(data, clazz);
                    if (resp.isSuccess()) {
                        Logger.t(requestTag).d("接口" + requestTag + ">>>>>>请求成功");
                        listener.onResponseSuccess(resp, requestTag);
                    } else {
                        Logger.t(requestTag).e("接口" + requestTag + ">>>>>>请求失败");
                        resp.isError(resp.resultCode);
                        listener.onResponseFail(requestTag,resp.resultCode);
                    }
                }else{
                    BaseResponse p = new BaseResponse();
                    p.setResultCode(code);
//                    p.isError(p.resultCode);
                    listener.onResponseFail(requestTag, code);
                }

            } catch (Exception ex) {
                try {
                    JSONObject object = new JSONObject(data);
                    int code = object.getInt("resultCode");
                    if(code == 1003){
                        BaseResponse p = new BaseResponse();
                        p.setResultCode(1003);
                        listener.onResponseSuccess(p, "1003");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onResponseFail(requestTag,5);
                    Logger.t(requestTag).e("接口" + requestTag + ">>>解析错误" + ex.getMessage() + "\n>>>code : " + HttpResponseListener.RESPONSE_DONE);
                }
//                listener.onResponseFail(requestTag);
                Logger.t(requestTag).e("接口" + requestTag + ">>>解析错误" + ex.getMessage() + "\n>>>code : " + HttpResponseListener.RESPONSE_DONE);
            }
        }
    }

    @Override
    public void onFailure(final int status, final Header[] headers, final byte[] body, final Throwable e) {
        String data = body == null ? "" : new String(body);
        Logger.t(requestTag).e("接口" + requestTag + ">>>>>>onFailure方法 status = " + status + ", body = " + data);
        listener.onResponseFail(requestTag,4);
        if (e != null) {
            Logger.t(requestTag).e("接口" + requestTag + ">>>>>>onFailure方法, Throwable??" + e.toString());
            e.printStackTrace();
        }

        if (status == -1) {
            Logger.t(requestTag).e("接口" + requestTag + ">>>>>>RESPONSE_ERROR  : " + data);
        } else {
            Logger.t(requestTag).e("接口" + requestTag + ">>>>>>RESPONSE_FAIL  : " + data);
        }
    }

}
